<?php
/**
* product相关逻辑
*
* @author     Robot
* @version    1.0
* @package    Base Framework
* @access     public
* @copyright  商业软件,受著作权保护
* @link       http://***
*/
class dataModule extends Base
{
	/**
	 * 构造函数
	 *
	 */
	public function __construct(){}

	/**
	 * 增加
	 * 
	 */
	public function add($item)
	{
		foreach($item as $key => $val)
		{
			if($key < 2)
			{
				continue;
			}
			$item[$key] = $val == '●' ? 1 : 0;
		}

		$oDto = new DTO_Product_Library();
		$oDto->name = $item[0];
		$oDto->pinpai = $item[1];
		$oDto->f1 = $item[2];
		$oDto->f2 = $item[3];
		$oDto->f3 = $item[4];
		$oDto->f4 = $item[5];
		$oDto->f5 = $item[6];
		$oDto->f6 = $item[7];
		$oDto->f7 = $item[8];
		$oDto->f8 = $item[9];
		$oDto->f9 = $item[10];
		$oDto->f10 = $item[11];
		$oDto->f11 = $item[12];
		$oDto->f12 = $item[13];
		$oDto->f13 = $item[14];
		$oDto->f14 = $item[15];
		$oDto->f15 = $item[16];
		$oDto->f16 = $item[17];
		$oDto->f17 = $item[18];
		$oDto->f18 = $item[19];
		$oDto->f19 = $item[20];
		$oDto->f20 = $item[21];
		$oDto->f21 = $item[22];
		$oDto->f22 = $item[23];
		$oDto->f23 = $item[24];
		$oDto->f24 = $item[25];
		$oDto->f25 = $item[26];
		$oDto->f26 = $item[27];
		$oDto->f27 = $item[28];
		$oDto->f28 = $item[29];
		$oDto->f29 = $item[30];
		$oDto->f30 = $item[31];
		$oDto->f31 = $item[32];
		$oDto->f32 = $item[33];
		$oDto->f33 = $item[34];
		$oDto->f34 = $item[35];
		$oDto->f35 = $item[36];
		$oDto->f36 = $item[37];
		$oDto->f37 = $item[38];
		$oDto->f38 = $item[39];
		$oDto->f39 = $item[40];
		$oDto->f40 = $item[41];
		$oDto->f41 = $item[42];
		$oDto->f42 = $item[43];
		$oDto->f43 = $item[44];
		$oDto->f44 = $item[45];
		$oDto->f45 = $item[46];
		$oDto->f46 = $item[47];
		$oDto->f47 = $item[48];
		$oDto->f48 = $item[49];
		$oDto->f49 = $item[50];
		$oDto->f50 = $item[51];
		$oDto->state = 1;
		$oDto->lasttime = time();

		$oDmo = new DMO_Product_Library();

		$aKey = array("name"=>$oDto->name, "pinpai"=>$oDto->pinpai);
		$aR = $oDmo->getRow($aKey);

		//和上一次重复
		if($aR)
		{
			return false;
		}

		if(!$oDmo->add($oDto))
		{
			throw new MyException("Err001");
		}

		return true;
	}

	/**
	 * 修改字段
	 * 
	 */
	public function edit($id_filed, $value)
	{
		$aField = array("999"=>"name","998"=>"pinpai");
		$fieldArr = array('a1','a2','a3','a4','a5','a6','a7','a8','a9','a10','a11','a12','ac0','ac1','ac2','ac3','ac4','ac5','ac6','ac7','ab1','ab2','ab3','ab4','ab5','ab6','aa0','aa1','aa2','aa3','aa4','aa5','aa6','ae1','ae2','ah1','ah2','ah3','ah4','ah5','ah6','ai1','ai2','ai3','ai4','ai5','ai6','ai7','ag1','ag2','ag3','ak1','ak2','ak3','b1','b2','b3','b4','b5','b6','b7','b8','b9','b10','b11','b12','b26','b27','b13','b14','b15','b16','b18','b19','b20','b21','b22','b23','b24','b25','ba1','ba2','ba3','bb1','bb2','bb3','bc1','bc2','bc3','bd1','bd2','bd3','be1','be2','be3','bf1','bf2','bf3','bg1','bg2','bg3','bh1','bh2','bh3','c1','c2','c3','c4','c5','c6','c7','c8','c9','c10','c11','c12','c13','c14','c15','c16','c17','c18','c29','c30','cc1','cc2','z1','z2','z3','z4','x1','x2','x3','x4','x5','af1','af2');
		$tmp = explode("_", $id_filed);
		//合法性检查
		if(!is_numeric($tmp[0]) || $tmp[0] <= 0 || $tmp[1] > 1000 || $tmp[1] < 0 || !in_array($value, array(0,1)))
		{
			return false;
		}
		$id = $tmp[0];
		if(in_array($tmp[1], array("998", "999")))
		{
			$field = $aField[$tmp[1]];
		}
		else
		{
			$field = $fieldArr[$tmp[1]];
		}

		$mysql = new PDOMySQL("xuanyou", "product_library");
		$sql  = "update product_library set {$field}='{$value}' where id='{$tmp[0]}'";
		if($mysql->exec($sql) === false)
		{
			throw new Exception("Err001");
		}

		$this->admin_managerModule->addLog(10, "编辑", "[油品]把id：{$id}的字段：{$field}改为：{$value}");

		return true;
	}

	/**
	* 列出日志
	*
	* @access public
	* @param  void
	* @return void
	*/
	public function listdata($aKey, $oPage)
	{
		$aOrder = array("id"=>"desc");

		$oDmo = new DMO_Product_Library();
		$aRes = $oDmo->getPage($oPage->getBegin(), $oPage->PerPage, $aKey, $aOrder);
		$oPage->Count = $oDmo->getTotal();

		return $aRes;
	}

	/**
	 * 关闭
	 * 
	 */
	public function setState($id, $state)
	{
		//标记为已删除
		$oDto = new DTO_Product_Library();
		$oDto->id = $id;
		$oDto->state = $state;

		$oDmo = new DMO_Product_Library();
		$oDmo->setField("state");
		if(!$oDmo->modify($oDto))
		{
			throw new Exception("Err001");
		}

		//记录日志
		$opt = $state == 1 ? "开放" : "关闭";
		$this->admin_managerModule->addLog(10, $opt, "[油品]id=".$id);

		return true;
	}

	/**
	* 检查数据格式
	*
	* @access public
	* @param  void
	* @return void
	*/
	public function check($arr)
	{
		if(!$arr[0])
		{
			$error[] = "规格型号必填";
		}
		if(!$arr[1])
		{
			$error[] = "品牌必填";
		}

		foreach($arr as $key => $val)
		{
			if($key < 2)
			{
				continue;
			}
			if($val != "" && $val != '●')
			{
				$error[] = "v".($key - 1)."只能是●或为空";
			}
		}

		if(!empty($error))
		{
			return join("；", $error);
		}

		return true;
	}
}

?>